package edu.ped.android.view;

import java.util.Observable;
import java.util.Observer;

import edu.ped.android.nfc.InitialisationActivity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class InitMatriceView extends View implements Observer{

	private int x;
	private int y;
	private MyNumberPicker setPickerX;
	private MyNumberPicker setPickerY;
	
	public InitMatriceView(Context context, AttributeSet attrs) {
		super(context, attrs);
		x = InitialisationActivity.DEFAULT_X;
		y = InitialisationActivity.DEFAULT_Y;
		setBackgroundColor(Color.WHITE);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		int tailleX=  (getWidth() /  x);
		int tailleY=  (getHeight() /   y);
		Paint p = new Paint();
		p.setColor(Color.BLACK);
		p.setTextSize(30);
		int cpt = 0;
		for(int i =0;i<y;i++)
			if(i%2==0)
				for(int j =0;j<x;j++){
					canvas.drawText(""+cpt++, j*tailleX+tailleX/2-20, i*tailleY+tailleY/2, p);
				}
			else
				for(int j=x-1;j>=0;j--){
					canvas.drawText(""+cpt++, j*tailleX+tailleX/2-20, i*tailleY+tailleY/2, p);
				}
	}

	public void setX(int x) {
		this.x = x;
		invalidate();
	}

	public void setY(int y) {
		this.y = y;
		invalidate();
	}

	public void setPickerX(MyNumberPicker pickerX) {
		this.setPickerX = pickerX;
	}

	public void setPickerY(MyNumberPicker pickerY) {
		this.setPickerY = pickerY;
	}

	public void update(Observable observable, Object data) {
		Log.i("PED", "UPDATE");
		if(observable == setPickerX.getObservable())
			setX(setPickerX.getValue());
		else if(observable == setPickerY.getObservable())
			setY(setPickerY.getValue());
	}
}
